package com.hp.bon.sgw.stdbss30.bizSlot;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.bean.CommonDynQueryResponse;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @PROJECT_NAME: SGW_2.1
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/19 15:31
 */
public class CommonDynQueryBizSlot implements BizSlot {
    private static final Logger LOG = LoggerFactory.getLogger(CommonDynQueryBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        Map<String, Object> msgBody = new HashMap<>();

        String salesIndentId = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Sales_Indent_Id");
        if (salesIndentId.isEmpty()) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            return bizResult;
        }
        String[] split = salesIndentId.split("\\|");
        String accNbr = split[0];
        String objValue = split[0];
        String billingCycleId = split[2];
        msgBody.put("billingCycleId", billingCycleId);
        msgBody.put("accNbr", accNbr);

        Map<String, String> operAttrStruct = new HashMap<>();
        operAttrStruct.put("operServiceId", "1");
        operAttrStruct.put("lanId", "8420100");
        operAttrStruct.put("operPost", "0");
        operAttrStruct.put("operOrgId", "0");
        operAttrStruct.put("staffId", "0");
        operAttrStruct.put("operTime", "0");
        msgBody.put("operAttrStruct", operAttrStruct);

        Map<String, String> svcObjectStruct = new HashMap<>();
        svcObjectStruct.put("objValue", objValue);
        svcObjectStruct.put("objAttr", "2");
        svcObjectStruct.put("objType", "3");
        svcObjectStruct.put("dataArea", "0");
        msgBody.put("svcObjectStruct", svcObjectStruct);


        String msgBodyStr = JSON.toJSONString(msgBody);

        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
        // String  url="https://0f1f3a3d-15fa-4a03-bbf1-003764d88a79.mock.pstmn.io/monthlyBill";
        LOG.info("request body is {}", msgBodyStr.toString());
        LOG.info("webserviceUrl is {}", url);
        String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), url);
        LOG.info("response is {}", respStr);

        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            return bizResult;
        }
        CommonDynQueryResponse CommonDynQueryResponse = new Gson().fromJson(respStr, CommonDynQueryResponse.class);
        TreeMap<Object, Object> respDataXml = new TreeMap<>();
        if (CommonDynQueryResponse != null) {
            respDataXml.put("Result", CommonDynQueryResponse.getResultCode());
            respDataXml.put("Para_Field_Result", CommonDynQueryResponse.getResultMsg().isEmpty() ? "" : CommonDynQueryResponse.getResultMsg());
            TreeMap<Object, Object> BillInfoMap = new TreeMap<>();
            BillInfoMap.put("State", CommonDynQueryResponse.getStopState());
            respDataXml.put("Bill_Information", BillInfoMap);
        } else {
            respDataXml.put("Result", null);
            respDataXml.put("Para_Field_Result", null);
            respDataXml.put("Bill_Information", null);
        }
        String respXmlStr = "";
        try {
            String respDataXmlStr = HttpClientUtils.mapToSignXml(respDataXml);
            respXmlStr = HttpClientUtils.directAck(xmlMessage, respDataXmlStr);
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            LOG.error("Stitching XML error", e);
            return bizResult;
        }
        bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        bizResult.setHttpBody(respXmlStr);
        return bizResult;
    }

    public static void main(String[] args) {

    }
}
